package com.org.controller;


import com.org.Msg.Msg;
import com.org.entity.User;
import com.org.serviceImpl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘文鑫
 * @since 2020-12-29
 */
@RestController
public class UserController {

    @Autowired
    UserServiceImpl userServiceImpl;

    /**
     * 注册操作
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Msg register(User user){
        return userServiceImpl.register(user);
    }

    /**
     * 当权限为ROLE_ADMIN时方可访问，否则抛出权限不足异常
     * @return
     */
    @GetMapping("/index")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Msg index(){
        Msg msg=Msg.success("查询成功！！！");
        msg.setData("index");
        return msg;
    }

    /**
     * 当权限为ROLE_USER时方可访问，否则抛出权限不足异常
     * @return
     */
    @GetMapping("/hello")
    @PreAuthorize("hasRole('ROLE_USER')")
    public Msg hello(){
        Msg msg=Msg.success("查询成功！！！");
        msg.setData("hello");
        return msg;
    }
}

